//
//  DynamicTypeThemeVC.swift
//  ACFrameworkSwift_Example
//
//  Created by c5342925 on 2022/3/31.
//  Copyright © 2022 CocoaPods. All rights reserved.
//

import Foundation
import ACFrameworkSwift
import UIKit

class DynamicTypeThemeVC: UIViewController {
    
    var titleLabel: UILabel = {
        let lab = UILabel()
        lab.text = "Tweaking The System Fonts"
        lab.textAlignment = .center
        lab.numberOfLines = 0
        return lab
    }()
    
    var button: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("这是普通的按钮", for: .normal)
        btn.setTitle("这是选中的按钮", for: .selected)
        btn.addTarget(self, action: #selector(didClickButton(_:)), for: .touchUpInside)
        return btn
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if #available(iOS 11.0, *) {
            navigationItem.largeTitleDisplayMode = .never
        }
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "切换主题", style: .done, target: self, action: #selector(switchThemeAction))
        
        view.theme.backgroundColor = UIColor.preferredDynamicColor(colorStyle: .primaryBackground)
        titleLabel.theme.backgroundColor = UIColor.preferredDynamicColor(colorStyle: .teal2)
        titleLabel.theme.textColor = UIColor.preferredDynamicColor(colorStyle: .primaryLabel)
        
        button.theme.backgroundColor = UIColor.preferredDynamicColor(colorStyle: .teal2)
        button.theme.setTitleColor(UIColor.preferredDynamicColor(colorStyle: .secondaryLabel), for: .normal)
        button.theme.setTitleColor(UIColor.preferredDynamicColor(colorStyle: .indigo1), for: .selected)
        
        view.addSubview(titleLabel)
        view.addSubview(button)
    }
    
    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        let labX: CGFloat = 20
        let labWidth: CGFloat = UIScreen.main.bounds.size.width - 2 * labX
        let maxHeight = CGFloat.greatestFiniteMagnitude
        var labY: CGFloat = 100
        
        let titleSize = titleLabel.systemLayoutSizeFitting(CGSize(width: labWidth, height: maxHeight))
        
        titleLabel.frame = CGRect(x: labX, y: labY, width: labWidth + 15, height: titleSize.height + 15)
        
        labY += titleSize.height + 50
        
        button.frame.size = CGSize(width: 150, height: 60)
        button.center = view.center
    }
    
    deinit {
        print("~~ DynamicTypeThemeVC deinit  ")
    }
    
    @objc func didClickButton(_ sender: UIButton) {
        sender.isSelected = !sender.isSelected
    }
    
    @objc func switchThemeAction() {
        let theme: ACThemeStyle = ACThemeManager.share.currentTheme == .light ? .dark : .light
        ACThemeManager.share.switchTheme(theme: theme)
    }
}

